﻿Velsn={};
Velsn.ClassUtil={
	extend: function(destination,source){
		if(destination&&source){
			for(var property in source){
				destination[property]=source[property];
			}
			if(source.hasOwnProperty&&source.hasOwnProperty("toString")){
				destination.toString=source.toString;
			}
		}
		return destination;
	},
	applyDefaults: function(to,from){
		for(var key in from){
			if(to[key]==null){
				to[key]=from[key];
			}
		}
	}
};

Velsn.Class=function(){
	var extended={};
	var parent;
	for(var i=0;i<arguments.length;i++){
		if(typeof arguments[i]=="function"){
			parent=arguments[i].prototype;
		}else{
			parent=arguments[i];
		}
		Velsn.ClassUtil.extend(extended,parent);
	}
	for(var i=0;i<arguments.length;i++){
		delete arguments[i];
	}
	var Class=function(){
		if(typeof this.construct=="function"){
			this.construct.apply(this,arguments);
	}};
	Class.prototype=extended;
	Class.prototype.constructor=Class;
	return Class;
};
Velsn.VelsnUtil = {
	getPageSize : function() {
		var scrW, scrH;
		if (window.innerHeight && window.scrollMaxY) {
			// Mozilla
			scrW = window.innerWidth + window.scrollMaxX;
			scrH = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			// all but IE Mac
			scrW = document.body.scrollWidth;
			scrH = document.body.scrollHeight;
		} else if (document.body) { // IE Mac
			scrW = document.body.offsetWidth;
			scrH = document.body.offsetHeight;
		}

		var winW, winH;
		if (window.innerHeight) { // all except IE
			winW = window.innerWidth;
			winH = window.innerHeight;
		} else if (document.documentElement
				&& document.documentElement.clientHeight) {
			// IE 6 Strict Mode
			winW = document.documentElement.clientWidth;
			winH = document.documentElement.clientHeight;
		} else if (document.body) { // other
			winW = document.body.clientWidth;
			winH = document.body.clientHeight;
		}

		// for small pages with total size less then the viewport
		var pageW = (scrW < winW) ? winW : scrW;
		var pageH = (scrH < winH) ? winH : scrH;

		return {
			pageW : pageW,
			pageH : pageH,
			winW : winW,
			winH : winH
		};
	},

	getPageScroll : function() {
		var x, y;
		if (window.pageYOffset) {
			// all except IE
			y = window.pageYOffset;
			x = window.pageXOffset;
		} else if (document.documentElement
				&& document.documentElement.scrollTop) {
			// IE 6 Strict
			y = document.documentElement.scrollTop;
			x = document.documentElement.scrollLeft;
		} else if (document.body) {
			// all other IE
			y = document.body.scrollTop;
			x = document.body.scrollLeft;
		}
		return {
			x : x,
			y : y
		};
	}
};
Velsn.VelsnDiv = Velsn.Class({
	
	construct : function(closeCB){
		var me = this;
		me._closeCB = closeCB;
		me._modalDiv = $('<div id="modalDiv" style="background:#000000;position:absolute;left:0px;top:0px;z-index:1000;width:98%;height:98%;filter:alpha(opacity=30);opacity: 0.3;"></div>');
		me._div = $("<div></div>");
		me._div.css({position: "absolute",width: "100%", "z-index": 1001,left:"0px",top:"10%"});
	},
	
	setContext : function(div){
		div.appendTo(this._div);
	},
	
	offset : function(left,top) {
		var me = this;
		if (left.charAt(left.length-1) == "%") 
			me._div.css("left",left);
		else
			me._div.css("left",left+"px");
		if (top.charAt(top.length-1) == "%") 
			me._div.css("top",top);
		else
			me._div.css("top",(top-20)+"px");
	},
	
	setTitle : function(title) {
		this._div.find("#panelTitle").html(title);
	},

	width : function(width) {
		this._div.css("width", isNaN(width) ? width : width + "px");
	},

	height : function(height) {
		this._div.css("height", isNaN(height) ? height : height + "px");
	},

	setZIndex : function(index) {
		var me = this;
		me._modalDiv.css("z-index", index);
		me._div.css("z-index", index + 1);
	},

	enableModal : function() {
		this._isShowModal = true;
	},

	show : function(isShowModal) {
		var me = this;
		if (me._isShowModal) {
			me._modalDiv.prependTo($("body"));
			var pageSize = Velsn.VelsnUtil.getPageSize();
			var point = Velsn.VelsnUtil.getPageScroll();
			me._modalDiv.height(pageSize.pageH);
			me._modalDiv.width(pageSize.pageW);
		}
		me._div.prependTo($("body"));
		me._modalDiv.show();
		me._div.show();
	},

	hide : function() {
		var me = this;
		me._modalDiv.hide();
		me._div.hide();
		if (me._closeCB) 
			me._closeCB();
	},

	div : function() {
		return this._div;
	},

	close : function() {
		var me = this;
		me._modalDiv.hide();
		me._modalDiv.remove();
		me._div.hide();
		me._div.remove();
		if (me._closeCB) 
			me._closeCB();
	}

});
Velsn.LoadingDiv = Velsn.Class({
	
	construct : function(){
		var me = this;
		me._contexts = [];
		me._requestCount = 0;
		me._div = $('<div align="center"><img src="images/loading.gif" width="50" height="50" title="加载中..."></img>'
		+'<br/><span style="color:#000000;background-color:#F1471F;"><span id="dataType" style="color:#2257D6;"></span></span></div>');
		me._velsnDiv = new Velsn.VelsnDiv();
		me._velsnDiv.setContext(me._div);
		me._velsnDiv.setZIndex(1002);
		me._velsnDiv.enableModal();
	},
	
	setContext : function(context) {
		this._div.find("#dataType").html(context);
	},
	
	show : function(context) {
		var me = this;
		if (context) {
			me.setContext(context);
			me._contexts[me._contexts.length] = context;
		}
		me._requestCount ++;
		me._velsnDiv.show();
	},
	
	hide : function(context) {
		var me = this;
		if (context) 
			me._deleteContext(context);
		me._requestCount --;
		if (me._requestCount <= 0) 
			me._velsnDiv.hide();
	},
	
	setZIndex : function(index) {
		this._velsnDiv.setZIndex(index);
	},
	
	offset : function(left,top) {
		this._velsnDiv.offset(left,top);
	},
	
	_showLoadingDiv : function(){
		var me = this;
		me._requestCount ++;
		me._velsnDiv.show();
	},
	
	_deleteContext : function(context) {
		var me = this;
		for ( var i = 0; i < me._contexts.length; i++) {
			if (me._contexts[i] == context) {
				me._contexts[i] = me._contexts[me._contexts.length-1];
				me._contexts.pop();
				me.setContext(me._contexts[0]);
				return;
			}
		}
	},
	
	_hideLoadingDiv : function(){
		var me = this;
		me._requestCount --;
		if (me._requestCount <= 0) 
			me._velsnDiv.hide();
	}
	
});

jQuery.extend({
    decimalInput : function(input,intCount,decimalCount){
    	var oldValue = 0;
    	$(input).keyup(function(event){
    		var obj = event.srcElement ? event.srcElement : event.target;
    		if(isNaN(obj.value)){
				obj.value = oldValue;
			}else{
				var intValue = "";
				var decimalValue = "";
				if(obj.value.indexOf(".") != -1){
					var number = obj.value.split(".");
					intValue = number[0];
					decimalValue = number[1];
				}else{
					intValue = obj.value;
				}
				if(intValue.length > intCount || decimalValue.length > decimalCount){
					obj.value = oldValue;
				}else if(intValue.length == intCount && intValue.charAt(0) == '0'){
					obj.value = oldValue;
				}else{
					oldValue = obj.value;
				}
				
			}
    	});
    }
});

function loadScript(id, fileUrl){ 
	var scriptTag = document.getElementById( id ); 
	var head = document.getElementsByTagName('HEAD').item(0); 
	var script= document.createElement("script"); 
	if(scriptTag) head.removeChild( scriptTag ); 
	script.id = id; 
	script.type = "text/javascript"; 
	script.src=fileUrl ; 
	head.appendChild(script); 
} 